﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Library;
using System.Data;

/*
-- =============================================
-- Author:		Duong Hoang Vinh
-- Create date: 8/11/2011
-- Description:	Detail Class
-- =============================================
*/

namespace BusinessLayer
{
   public class Detail
    {
    #region "Variables and Properties"
    //Variables
        private int detail_ID;        
        private string category_ID;        
        private string description;        
        private int status;        

    //Properties
        public int Detail_ID
        {
            get { return detail_ID; }
            set { detail_ID = value; }
        }

        public string Category_ID
        {
            get { return category_ID; }
            set { category_ID = value; }
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public int Status
        {
            get { return status; }
            set { status = value; }
        }
    #endregion

    #region "Initial Method"
        public Detail() { }

        public Detail(SqlDataReader sdr)
        {
            if (sdr != null && !sdr.IsClosed)
            {
                Detail_ID = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    Category_ID = sdr.GetString(1);
                if (!sdr.IsDBNull(2))
                    Description = sdr.GetString(2);
                if (!sdr.IsDBNull(3))
                    Status = sdr.GetInt32(3);
            }
        }
    #endregion

    #region "Bussiness Methods"
    //Get All Detail
        public static List<Detail> GetAll()
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Detail_SHOWALL";
            List<Detail> lstDetail = new List<Detail>();
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);

            while (sdr.Read())
            {
                lstDetail.Add(new Detail(sdr));
            }

            return lstDetail;
        }

    //Get One Detail
        public static Detail GetOne(int idDetail)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Detail_SHOWONE";
            Detail detail = new Detail();
            SqlDataReader sdr;
            
            dbh.AddParameter("@Detail_ID", idDetail);
            sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);

            if (sdr.Read())
            {
                detail.Detail_ID = sdr.GetInt32(0);
                if (sdr.IsDBNull(1))
                    detail.Category_ID = sdr.GetString(1);
                if (sdr.IsDBNull(2))
                    detail.Description = sdr.GetString(2);
                if (sdr.IsDBNull(3))
                    detail.Status = sdr.GetInt32(3);
            }

            return detail;
        }

    //Insert Detail
        public static void Insert(Detail detail)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Detail_INSERT";

            dbh.AddParameter("@Category_ID", detail.Category_ID);
            dbh.AddParameter("@Description", detail.Description);

            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

    //Update Detail
        public static void Update(Detail detail)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Detail_UPDATE";

            dbh.AddParameter("@Detail_ID", detail.Detail_ID);
            dbh.AddParameter("@Category_ID", detail.Category_ID);
            dbh.AddParameter("@Description", detail.Description);

            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

    //Delete Detail
        public static void Delete(int idDetail)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Detail_DELETE";

            dbh.AddParameter("@Detail_ID", idDetail);
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
    #endregion
    }
}
